;; Tests of non-robust numeric functions
;; Copyright (C) 2022  Rocks Mazama
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.numeric.max-subarray-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.numeric.max-subarray :refer :all]))

(deftest degenerate-test
  (is (nil? (max-subarray nil)))
  (is (nil? (max-subarray ())))
  (is (nil? (max-subarray []))))

(deftest all-negative-test
  (is (= (max-subarray [-1 -2 -3]) [0 1 -1]))
  (is (= (max-subarray [-6 -4 -5]) [1 1 -4]))
  (is (= (max-subarray [-8 -9 -7]) [2 1 -7])))

(deftest all-positive-test
  (let [examples [[37]
                  [4 2]
                  [7 9 3]]]
    (doseq [coll examples]
      (is (= (max-subarray coll) [0 (count coll) (reduce + coll)])))))

(deftest small-test
  (testing "https://en.wikipedia.org/wiki/Maximum_subarray_problem"
    (is (= (max-subarray [-2, 1, -3, 4, -1, 2, 1, -5, 4]) [3 4 6])))

  (testing "https://www.interviewbit.com/blog/maximum-subarray-sum/"
    (is (= (max-subarray [-2 3 -1 2]) [1 3 4]))
    (is (= (max-subarray [-3 -4 5 -1 2 -4 6 -1]) [2 5 8]))))
